/* Data analysis of Baseline Data for Nepal CCF: Household */

local folder "C:\Users\mlevere\OneDrive - Mathematica\Documents\Projects\Nepal\"

/* Merge together all the sections into one big dataset 
   Rename all the variables along the way */
use "`folder'/Data/Baseline_Raw/SEC01", clear
rename q01_101 round_num
rename q01_102 co_name
rename q01_103 co_code
rename q01_104 house_sn
rename q01_105a num_elig_women
rename q01_105b int_elig_women
rename q01_106 co_mem_name
rename q01_107dd interview_day
rename q01_107mm interview_month
rename q01_107yy interview_year
rename q01_108 interviewer_name
rename q01_109 supervisor_name
rename q01_110 editor_name
rename q01_111 dataentry_name
tempfile baseline_raw_hh
save "`baseline_raw_hh'", replace

merge 1:1 www hh using "`folder'/Data/Baseline_Raw/SEC02", nogen
rename q02_201 district
rename q02_202 vdc
rename q02_203a vdc_name
rename q02_203b vdc_code
rename q02_204 ward_num
rename q02_205 village_name
rename q02_206a1 main_respondent_name
rename q02_206a2 main_respondent_id
rename q02_206b1 second_respondent_name
rename q02_206b2 second_respondent_id
rename q02_207a hh_head_name
rename q02_207b hh_head_id
rename q02_208 hh_head_caste
rename q02_209 spouse_name
rename q02_210 houseleft_name
rename q02_211 houseright_name
rename q02_212a hh_survey_result
rename q02_213 religion
rename q02_214 tot_family
save "`baseline_raw_hh'", replace

use "`folder'/Data/Baseline_Raw/SEC03", clear
rename q03_idc id_code
rename q03_301 name
rename q03_302 relationship
rename q03_303 sex
rename q03_304a age_years
rename q03_304b age_months
rename q03_305 literacy
rename q03_306 school
rename q03_307 highest_grade
rename q03_308 marital_status
rename q03_309a occupation_main
rename q03_309b occupation_secondary
rename q03_310 residing
rename q03_311 eligible
by www hh, sort: egen num_hh_members_raw = count(id_code)
by www hh, sort: egen num_hh_members_residing_raw = count(id_code) if residing == 1
by www hh, sort: egen num_eligible_women_raw = count(id_code) if eligible == 1 & age_year > 5
by www hh, sort: egen num_eligible_kids_raw = count(id_code) if eligible == 1 & age_year <= 5
by www hh, sort: egen num_hh_members = max(num_hh_members_raw)
by www hh, sort: egen num_hh_members_residing = max(num_hh_members_residing_raw)
by www hh, sort: egen num_eligible_women = max(num_eligible_women_raw)
by www hh, sort: egen num_eligible_kids = max(num_eligible_kids_raw)
keep if id_code == 1
gen hh_head_male = sex == 1
gen hh_head_age = age_years
keep www hh num_hh_members num_hh_members_residing num_eligible_women num_eligible_kids hh_head_male hh_head_age
tempfile SEC03_hh
save "`SEC03_hh'", replace
merge 1:1 www hh using "`baseline_raw_hh'", nogen
save "`baseline_raw_hh'", replace

use "`baseline_raw_hh'", clear
merge 1:1 www hh using "`folder'/Data/Baseline_Raw/SEC04A", nogen
rename q04_401 hh_occupy_only
rename q04_402 ownership
rename q04_403 rooms
rename q04_404 stories
rename q04_405 separate_kitchen
rename q04_406 flooring
rename q04_407 outside_wall
rename q04_408 roof
rename q04_409a reg_electricity
rename q04_409b power_gen
rename q04_409c solar_power
rename q04_410 water_source
rename q04_411 water_mins
rename q04_412a fuel_main
rename q04_412b fuel_second
rename q04_412c fuel_third
rename q04_413 firewood_mins
rename q04_414 toilet
rename q04_415 market_mins
rename q04_416 own_land
save "`baseline_raw_hh'", replace

use "`folder'/Data/Baseline_Raw/SEC04B", clear
/* For land, convert all land into one variable of the same unit: Ropanis
   Conversion factors: 1 bigha = 20 kattha = 400 dhur
                       1 ropani = 16 ana = 100 paisa
                       1 bigha = 13.31 ropani */
gen land = q04_417c + q04_417d/16 + q04_417e/100 if q04_417b == 1
replace land = (q04_417c + q04_417d/20 + q04_417e/400)*13.31 if q04_417b == 2
drop q04_417c q04_417d q04_417e q04_417b
reshape wide land, i(www hh) j(q04_417a)
rename land1 hh_land
rename land2 farmland
rename land3 bari
rename land4 baran
tempfile SEC04B_wide
save "`SEC04B_wide'", replace

use "`baseline_raw_hh'", clear
merge 1:1 www hh using "`SEC04B_wide'", nogen
save "`baseline_raw_hh'", replace

merge 1:1 www hh using "`folder'/Data/Baseline_Raw/SEC04C", nogen
rename q04_418 veg_garden
rename q04_419 veg_garden_purpose
rename q04_420 own_livestock
save "`baseline_raw_hh'", replace

use "`folder'/Data/Baseline_Raw/SEC05A", clear
drop if q05_501a == .
reshape wide q05_501b, i(www hh) j(q05_501a)
local expends "fuel clothes personal_care transport entertain newspaper educ_services health_services legal house_repair religious gifts electricity hh_goods agri_goods land"
local i = 1
local n : word count `expends'
while `i' < = `n' {
local change : word `i' of `expends'
rename q05_501b`i' expend_`change'
label variable expend_`change' "Monthly Expenditure: `change' (NRs)"
local i = `i' + 1
}
tempfile SEC05A_wide
save "`SEC05A_wide'", replace

use "`baseline_raw_hh'", clear
merge 1:1 www hh using "`SEC05A_wide'", nogen
save "`baseline_raw_hh'", replace

use "`folder'/Data/Baseline_Raw/SEC05B1", clear
drop if q05_502a == .
reshape wide q05_502b, i(www hh) j(q05_502a)
local incomes "agri nonagri hometown domestic foreign oldage widow disability oth_protection interest prize"
local i = 1
local n : word count `incomes'
while `i' < = `n' {
local change : word `i' of `incomes'
rename q05_502b`i' income_`change'
label variable income_`change' "Annual Income: `change' (NRs)"
local i = `i' + 1
}
tempfile SEC05B1_wide
save "`SEC05B1_wide'", replace

use "`baseline_raw_hh'", clear
merge 1:1 www hh using "`SEC05B1_wide'", nogen
save "`baseline_raw_hh'", replace

merge 1:1 www hh using "`folder'/Data/Baseline_Raw/SEC05B2", nogen
rename q05_503 num_borrowed_loans
rename q05_504 recentloan_interest
rename q05_505 lent_loans
save "`baseline_raw_hh'", replace

use "`folder'/Data/Baseline_Raw/SEC05C", clear
drop if q05_506a == .
reshape wide q05_506b1 q05_506b2, i(www hh) j(q05_506a)
local foods "rice beatenrice maize wheat barley millet pulses pea othbeans fish meat egg milk curd powdermilk ghee honey vegghee oil potato onion greenveg othveg fruitsnuts salt othspice sugar sakhar sweets tea alcohol nonalcohol tobacco noodles othfoods"
local i = 1
local n : word count `foods'
while `i' < = `n' {
local change : word `i' of `foods'
rename q05_506b1`i' food_`change'_amt
rename q05_506b2`i' food_`change'_unit
label variable food_`change'_amt "Food recall: Quantity of `change'"
label variable food_`change'_unit "Food recall: Unit of `change'"
local i = `i' + 1
}
tempfile SEC05C_wide
save "`SEC05C_wide'", replace

use "`baseline_raw_hh'", clear
merge 1:1 www hh using "`SEC05C_wide'", nogen
save "`baseline_raw_hh'", replace

use "`folder'/Data/Baseline_Raw/SEC05D", clear
drop if q05_507a == .
reshape wide q05_507b q05_507c q05_507d, i(www hh) j(q05_507a)
local shocks "newjob inheritance hh_death injury losejob losebusiness livestockdie cropdisease theft drought"

local i = 1
local n : word count `shocks'
while `i' < = `n' {
local change : word `i' of `shocks'
rename q05_507b`i' shock_`change'
rename q05_507c`i' shock_`change'_who
rename q05_507d`i' shock_`change'_strength
label variable shock_`change' "Shocks: Has household experienced `change'?"
label variable shock_`change'_who "Shocks: Who did `change' affect?"
label variable shock_`change'_strength "Shocks: What was the strength of `change' shock?"
local i = `i' + 1
}
tempfile SEC05D_wide
save "`SEC05D_wide'", replace

use "`baseline_raw_hh'", clear
merge 1:1 www hh using "`SEC05D_wide'", nogen
save "`baseline_raw_hh'", replace

/* Merge in information on Treatment/Control */
use "`folder'/Data/cosampling.dta", clear
keep if sampco == 1
keep district vdc control_vdc info_vdc cash_vdc
by district vdc, sort: keep if _n == 1
rename vdc vdc_name
gen district_n = 1 if district == "Ramechhap"
replace district_n = 2 if district == "Sindhuli"
replace district_n = 3 if district == "Sarlahi"
replace district_n = 4 if district == "Rautahat"
drop district
rename district_n district
merge 1:m district vdc_name using "`baseline_raw_hh'", nogen
sort www hh

/* Generate one variable with all three treatment indicators, and label it,
   for the purpose of generating tables */
gen treatment = 1 if control_vdc == 1
replace treatment = 2 if info_vdc == 1
replace treatment = 3 if cash_vdc == 1

label define treatgroup 1 "Control" 2 "Info Only" 3 "Info + Cash"
label values treatment treatgroup

/* Generate the three comparison dummies so that we can do 2-way
   hypothesis testing */
gen cash_control = cash_vdc
replace cash_control = . if info_vdc == 1

gen info_control = info_vdc
replace info_control = . if cash_vdc == 1

gen cash_info = cash_vdc
replace cash_info = . if control_vdc == 1

egen expend_total = rowtotal(expend_fuel-expend_agri_goods)
egen income_total = rowtotal(income_agri-income_prize)


save "`folder'/Data/baseline_raw_hh", replace


/* Create the relevant files that will be used for the baseline balance tables:
   Want to create a flag for bad assets/income, and also want to flag the households that do not
   have eligible women/children responding */
   
use "`folder'/Data/baseline_raw_hh", clear

gen flag_spending = 0
replace flag_spending = 1 if expend_total > income_total
egen expend_total_cutoff = pctile(expend_total), p(99)
egen income_total_cutoff = pctile(income_total), p(99)
replace flag_spending = 1 if expend_total >= expend_total_cutoff
replace flag_spending = 1 if expend_total == 0
replace flag_spending = 1 if income_total > income_total_cutoff

merge 1:m www hh using "`folder'/Data/baseline_raw_women"

gen flag_missingwomen = _merge == 1
drop _merge

by www hh, sort: keep if _n == 1

keep www hh flag*

save "`folder'/Data/baseline_flag_hh", replace
